//
//  HouseCosignViewController.m
//  Finder
//
//  Created by cdm on 11-4-14.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#define kPickerViewTypeRepaymentPrecent 1
#define kPickerViewTypeRepaymentYear 2
#define kPickerViewTypeMoneyRate 3
#define kMoneyRateCosignTypeCommerce @"kMoneyRateCosignTypeCommerce"
#define kMoneyRateCosignTypeAccumulationFund @"kMoneyRateCosignTypeAccumulationFund"
#define kMoneyRateYear1to5 @"kMoneyRateYear1to5"
#define kMoneyRateYear5to30 @"kMoneyRateYear5to30"

#import "HouseCosignViewController.h"
#import "HouseCosignResultViewController.h"

@interface HouseCosignViewController ()

@property (nonatomic, assign) int pickerViewType;
@property (nonatomic, assign) int selectedRepaymentPrecentDataIndex;
@property (nonatomic, assign) int selectedRepaymentYearDataIndex;
@property (nonatomic, assign) int selectedMoneyRateDataIndex;
@property (nonatomic, retain) NSMutableArray *repaymentPrecentData;
@property (nonatomic, retain) NSMutableArray *repaymentYearData;
@property (nonatomic, retain) NSMutableArray *moneyRateData;

- (void)initRepaymentPrecentData;
- (void)initRepaymentYearData;
- (void)initMoneyRateData;
- (void)showHouseAreaCalculate;
- (void)showMoneyAmountCalcute;
- (void)showRepaymentPrecent;
- (void)showRepaymentYear;
- (void)showMoneyRate;
- (void)setSelectedRepaymentPrecentDataWithIndex:(int)index;
- (void)setSelectedRepaymentYearDataWithIndex:(int)index;
- (void)setSelectedMoneyRateDataWithIndex:(int)index;
- (float)calculateHouseMoneyAmount;
- (float)calculateCosignAmount;
- (float)calculateAccrual;
- (float)calculateFirstMoney;
- (int)calculateRepaymentMonth;
- (float)calculateRepaymentPreMonth1;
- (float)calculateRepaymentAmount1;
- (NSMutableArray *)calculateRepaymentPreMonth2;
- (float)calculateRepaymentAmount2;

@end

@implementation HouseCosignViewController

@synthesize closeBarButtonItem; 
@synthesize repaymentMethodSegmentControl;
@synthesize repaymentCategorySegmentControl;
@synthesize calculateSegmentController;
@synthesize houseAreaView;
@synthesize moneyAmountView;
@synthesize calculateView;
@synthesize dataPickerView;
@synthesize houseAreaTextField;
@synthesize houseAreaPriceTextField;
@synthesize moneyAmountTextField;
@synthesize backgroundButton;
@synthesize repaymentPrecentButton;
@synthesize repaymentYearButton;
@synthesize moneyRateButton;
@synthesize calculateButton;
@synthesize pickerView;
@synthesize actionSheet;

@synthesize selectedRepaymentPrecentDataIndex;
@synthesize selectedRepaymentYearDataIndex;
@synthesize selectedMoneyRateDataIndex;
@synthesize pickerViewType;
@synthesize repaymentPrecentData;
@synthesize repaymentYearData;
@synthesize moneyRateData;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self initRepaymentPrecentData];
        [self initRepaymentYearData];
        [self initMoneyRateData];
    }
    
    return self;
}

- (void)dealloc
{
    [self.closeBarButtonItem release];
    [self.repaymentMethodSegmentControl release];
    [self.repaymentCategorySegmentControl release];
    [self.calculateSegmentController release];
    [self.houseAreaView release];
    [self.moneyAmountView release];
    [self.calculateView release];
    [self.dataPickerView release];
    [self.houseAreaTextField release];
    [self.houseAreaPriceTextField release];
    [self.moneyAmountTextField release];
    [self.backgroundButton release];
    [self.repaymentPrecentButton release];
    [self.repaymentYearButton release];
    [self.moneyRateButton release];
    [self.calculateButton release];
    [self.pickerView release];
    [self.actionSheet release];
    
    [self.repaymentPrecentData release];
    [self.repaymentYearData release];
    [self.moneyRateData release];
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:nil 
                                                      delegate:nil
                                             cancelButtonTitle:nil
                                        destructiveButtonTitle:nil
                                             otherButtonTitles:nil];
    [menu addSubview:self.dataPickerView];
    [menu setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
    self.actionSheet = menu;
    [menu release];
    
    [self setSelectedRepaymentPrecentDataWithIndex:5];
    [self setSelectedRepaymentYearDataWithIndex:19];
    [self setSelectedMoneyRateDataWithIndex:3];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.closeBarButtonItem = nil; 
    self.repaymentMethodSegmentControl = nil;
    self.repaymentCategorySegmentControl = nil;
    self.calculateSegmentController = nil;
    self.houseAreaView = nil;
    self.moneyAmountView = nil;
    self.calculateView = nil;
    self.dataPickerView = nil;
    self.houseAreaTextField = nil;
    self.houseAreaPriceTextField = nil;
    self.moneyAmountTextField = nil;
    self.backgroundButton = nil;
    self.repaymentPrecentButton = nil;
    self.repaymentYearButton = nil;
    self.moneyRateButton = nil;
    self.calculateButton = nil;
    self.pickerView = nil;
    self.actionSheet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Picker view data source

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
    
    if (self.pickerViewType == kPickerViewTypeRepaymentPrecent) {
        return [self.repaymentPrecentData count];
    } else if (self.pickerViewType == kPickerViewTypeRepaymentYear) {
        return [self.repaymentYearData count];
    } else if (self.pickerViewType == kPickerViewTypeMoneyRate) {
        return [self.moneyRateData count];
    }
    
    return 0;
}

#pragma mark - Picker view delegate

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSMutableArray *data = nil;
    if (self.pickerViewType == kPickerViewTypeRepaymentPrecent) {
        data = self.repaymentPrecentData;
    } else if (self.pickerViewType == kPickerViewTypeRepaymentYear) {
        data = self.repaymentYearData;
    } else if (self.pickerViewType == kPickerViewTypeMoneyRate) {
        data = self.moneyRateData;
    }    
    
    NSMutableDictionary *dataItem = [data objectAtIndex:row];
    NSString *label = [dataItem objectForKey:@"label"];
    
    return label;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (self.pickerViewType == kPickerViewTypeRepaymentPrecent) {
        [self setSelectedRepaymentPrecentDataWithIndex:row];
    } else if (self.pickerViewType == kPickerViewTypeRepaymentYear) {
        [self setSelectedRepaymentYearDataWithIndex:row];
    } else if (self.pickerViewType == kPickerViewTypeMoneyRate) {
        [self setSelectedMoneyRateDataWithIndex:row];
    }    
}

#pragma mark - Event

- (void)backgroundButtonOnClick:(id)sender
{
    [self.houseAreaTextField endEditing:YES];
    [self.houseAreaPriceTextField endEditing:YES];
    [self.moneyAmountTextField endEditing:YES];
}


- (void)calculateSegmentControllerOnClick:(id)sender
{
    int idx = calculateSegmentController.selectedSegmentIndex;
    
    if (idx == 0) {
        [self showHouseAreaCalculate];
    } else if (idx == 1) {
        [self showMoneyAmountCalcute];
    }
}

- (IBAction) repaymentPrecentButtonOnClick:(id)sender
{
    [self showRepaymentPrecent];
}

- (IBAction) repaymentYearButtonOnClick:(id)sender
{
    [self showRepaymentYear];
}

- (IBAction) moneyRateButtonOnClick:(id)sender
{
    [self showMoneyRate];
}

- (void)closeDataPickerViewButtonOnClick:(id)sender
{
    [self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}

- (void)calculateButtonOnClick:(id)sender
{
    HouseCosignResultViewController *targetController = [[HouseCosignResultViewController alloc] initWithNibName:@"HouseCosignResultView" bundle:nil];
    targetController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [targetController loadView];
    
    // 房款总额
    float houseMoneyAmount = [self calculateHouseMoneyAmount];
    NSString *houseMoneyAmountStr = [NSString stringWithFormat:@"%.02f 元", houseMoneyAmount];
    NSLog(@"房款总额 %@", houseMoneyAmountStr);
    targetController.houseMoneyAmountLabel.text = houseMoneyAmountStr;
    
    // 贷款总额
    float cosignAmount =[self calculateCosignAmount];
    NSString *cosignAmountStr = [NSString stringWithFormat:@"%.02f 元", cosignAmount];
    NSLog(@"贷款总额 %@", cosignAmountStr);
    targetController.cosignAmountLabel.text = cosignAmountStr;
    
    // 还款总额
    if (self.repaymentMethodSegmentControl.selectedSegmentIndex == 0) {
        float repaymentAmount1 = [self calculateRepaymentAmount1];
        NSString *repaymentAmountStr1 = [NSString stringWithFormat:@"%.02f 元", repaymentAmount1];
        NSLog(@"还款总额 等额本息 %@", repaymentAmountStr1);
        targetController.repaymentAmountLabel.text = repaymentAmountStr1;
    } else if (self.repaymentMethodSegmentControl.selectedSegmentIndex == 1){
        float repaymentAmount2 = [self calculateRepaymentAmount2];
        NSString *repaymentAmountStr2 = [NSString stringWithFormat:@"%.02f 元", repaymentAmount2];
        NSLog(@"还款总额 等额本金 %@", repaymentAmountStr2);
        targetController.repaymentAmountLabel.text = repaymentAmountStr2;
    }
        
    
    // 支付利息款
    float accrualAmount = [self calculateAccrual];
    NSString *accrualAmountStr = [NSString stringWithFormat:@"%.02f 元", accrualAmount];
    NSLog(@"支付利息款 %@", accrualAmountStr);
    targetController.accrualLabel.text = accrualAmountStr;
    
    // 首期付款
    float firstMoney = [self calculateFirstMoney];
    NSString *firstMoneyStr = [NSString stringWithFormat:@"%.02f 元", firstMoney];
    NSLog(@"首期付款 %@", firstMoneyStr);
    targetController.firstMoneyLabel.text = firstMoneyStr;

    // 贷款月数
    int repaymentMonth = [self calculateRepaymentMonth];
    NSString *repaymentMonthStr = [NSString stringWithFormat:@"%d 月", repaymentMonth];
    NSLog(@"贷款月数 %@", repaymentMonthStr);
    targetController.loanMonthLabel.text = repaymentMonthStr;

    // 月均还款
    if (self.repaymentMethodSegmentControl.selectedSegmentIndex == 0) {
        float repaymentPreMonth = [self calculateRepaymentPreMonth1];
        NSString *repaymentPreMonthStr = [NSString stringWithFormat:@"%.02f 元", repaymentPreMonth];
        NSLog(@"月均还款 等额本息 %@", repaymentPreMonthStr);
        targetController.repaymentPreMonthTextView.text = repaymentPreMonthStr;
    } else if (self.repaymentMethodSegmentControl.selectedSegmentIndex == 1){
        NSLog(@"月均还款 等额本金 ");        
        NSMutableArray *months = [self calculateRepaymentPreMonth2];
        NSMutableString *repaymentPreMonthStr = [[NSMutableString alloc] init];
        for (int i = 0; i < [months count]; i++) {
            float repaymentPreMonth = [[months objectAtIndex:i] floatValue];
            NSString *preMonthStr = [NSString stringWithFormat:@"第%d月 %.02f\r\n", i+1, repaymentPreMonth];
            [repaymentPreMonthStr appendString:preMonthStr];
            NSLog(@"第%d月 %f", i+1, repaymentPreMonth);
        }
        
        targetController.repaymentPreMonthTextView.text = repaymentPreMonthStr;
        [repaymentPreMonthStr release];
        
        
    }
  
    [self presentModalViewController:targetController animated:YES];
    [targetController release];
}

- (void)hideKeyboard:(id)sender
{
    [self.houseAreaTextField endEditing:YES];
    [self.houseAreaPriceTextField endEditing:YES];
    [self.moneyAmountTextField endEditing:YES];
}

#pragma mark - Init data

- (void)initRepaymentPrecentData
{
    NSMutableArray *data = [[NSMutableArray alloc] init];
    self.repaymentPrecentData = data;
    [data release];
    
    NSMutableDictionary *dataItem = nil;
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"2成" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:2] forKey:@"value"];
    [self.repaymentPrecentData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"3成" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:3] forKey:@"value"];
    [self.repaymentPrecentData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"4成" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:4] forKey:@"value"];
    [self.repaymentPrecentData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"5成" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:5] forKey:@"value"];
    [self.repaymentPrecentData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"6成" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:6] forKey:@"value"];
    [self.repaymentPrecentData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"7成" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:7] forKey:@"value"];
    [self.repaymentPrecentData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"8成" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:8] forKey:@"value"];
    [self.repaymentPrecentData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"9成" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:9] forKey:@"value"];
    [self.repaymentPrecentData addObject:dataItem];
    [dataItem release];
}

- (void)initRepaymentYearData
{
    NSMutableArray *data = [[NSMutableArray alloc] init];
    self.repaymentYearData = data;
    [data release];
    
    NSMutableDictionary *dataItem = nil;
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"1年（12期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:1] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"2年（24期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:2] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"3年（36期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:3] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"4年（48期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:4] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"5年（60期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:5] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"6年（72期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:6] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"7年（84期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:7] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"8年（96期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:8] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"9年（108期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:9] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"10年（120期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:10] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"11年（132期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:11] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"12年（144期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:12] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"13年（156期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:13] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"14年（168期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:14] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"15年（180期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:15] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"16年（192期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:16] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"17年（204期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:17] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"18年（216期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:18] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"19年（228期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:19] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"20年（240期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:20] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"25年（300期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:25] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"30年（360期）" forKey:@"label"];
    [dataItem setObject:[NSNumber numberWithInt:30] forKey:@"value"];
    [self.repaymentYearData addObject:dataItem];
    [dataItem release];
}


- (void)initMoneyRateData
{
    NSMutableArray *data = [[NSMutableArray alloc] init];
    self.moneyRateData = data;
    [data release];
    
    NSMutableDictionary *dataItem = nil;
    NSMutableDictionary *commerce = nil;
    NSMutableDictionary *accumulationFund = nil;
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"11年4月5日利率上限（1.1倍）" forKey:@"label"];
    commerce = [[NSMutableDictionary alloc] init];
    [commerce setObject: [NSNumber numberWithFloat:0.0732] forKey:kMoneyRateYear1to5];
    [commerce setObject:[NSNumber numberWithFloat:0.0748] forKey:kMoneyRateYear5to30];
    [dataItem setObject:commerce forKey:kMoneyRateCosignTypeCommerce];
    [commerce release];
    accumulationFund = [[NSMutableDictionary alloc] init];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.042] forKey:kMoneyRateYear1to5];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.047] forKey:kMoneyRateYear5to30];
    [dataItem setObject:kMoneyRateCosignTypeAccumulationFund forKey:accumulationFund];
    [accumulationFund release];
    //[dataItem setObject:[NSNumber numberWithInt:19] forKey:@"value"];
    [self.moneyRateData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"11年4月5日利率下限（85折）" forKey:@"label"];
    commerce = [[NSMutableDictionary alloc] init];
    [commerce setObject: [NSNumber numberWithFloat:0.0565] forKey:kMoneyRateYear1to5];
    [commerce setObject:[NSNumber numberWithFloat:0.0578] forKey:kMoneyRateYear5to30];
    [dataItem setObject:commerce forKey:kMoneyRateCosignTypeCommerce];
    [commerce release];
    accumulationFund = [[NSMutableDictionary alloc] init];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.042] forKey:kMoneyRateYear1to5];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.047] forKey:kMoneyRateYear5to30];
    [dataItem setObject:kMoneyRateCosignTypeAccumulationFund forKey:accumulationFund];
    [accumulationFund release];
    //[dataItem setObject:[NSNumber numberWithInt:18] forKey:@"value"];
    [self.moneyRateData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"11年4月5日利率下限（7折）" forKey:@"label"];
    commerce = [[NSMutableDictionary alloc] init];
    [commerce setObject: [NSNumber numberWithFloat:0.0466] forKey:kMoneyRateYear1to5];
    [commerce setObject:[NSNumber numberWithFloat:0.0476] forKey:kMoneyRateYear5to30];
    [dataItem setObject:commerce forKey:kMoneyRateCosignTypeCommerce];
    [commerce release];
    accumulationFund = [[NSMutableDictionary alloc] init];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.042] forKey:kMoneyRateYear1to5];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.047] forKey:kMoneyRateYear5to30];
    [dataItem setObject:kMoneyRateCosignTypeAccumulationFund forKey:accumulationFund];
    [accumulationFund release];
    //[dataItem setObject:[NSNumber numberWithInt:17] forKey:@"value"];
    [self.moneyRateData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"11年4月5日基准利率" forKey:@"label"];
    commerce = [[NSMutableDictionary alloc] init];
    [commerce setObject: [NSNumber numberWithFloat:0.0665] forKey:kMoneyRateYear1to5];
    [commerce setObject:[NSNumber numberWithFloat:0.068] forKey:kMoneyRateYear5to30];
    [dataItem setObject:commerce forKey:kMoneyRateCosignTypeCommerce];
    [commerce release];
    accumulationFund = [[NSMutableDictionary alloc] init];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.042] forKey:kMoneyRateYear1to5];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.047] forKey:kMoneyRateYear5to30];
    [dataItem setObject:kMoneyRateCosignTypeAccumulationFund forKey:accumulationFund];
    [accumulationFund release];
    //[dataItem setObject:[NSNumber numberWithInt:16] forKey:@"value"];
    [self.moneyRateData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"11年2月9日利率上限(1.1倍)" forKey:@"label"];
    commerce = [[NSMutableDictionary alloc] init];
    [commerce setObject: [NSNumber numberWithFloat:0.071] forKey:kMoneyRateYear1to5];
    [commerce setObject:[NSNumber numberWithFloat:0.0726] forKey:kMoneyRateYear5to30];
    [dataItem setObject:commerce forKey:kMoneyRateCosignTypeCommerce];
    [commerce release];
    accumulationFund = [[NSMutableDictionary alloc] init];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.04] forKey:kMoneyRateYear1to5];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.045] forKey:kMoneyRateYear5to30];
    [dataItem setObject:kMoneyRateCosignTypeAccumulationFund forKey:accumulationFund];
    [accumulationFund release];
    //[dataItem setObject:[NSNumber numberWithInt:15] forKey:@"value"];
    [self.moneyRateData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"11年2月9日利率下限(85折)" forKey:@"label"];
    commerce = [[NSMutableDictionary alloc] init];
    [commerce setObject: [NSNumber numberWithFloat:0.0528] forKey:kMoneyRateYear1to5];
    [commerce setObject:[NSNumber numberWithFloat:0.0561] forKey:kMoneyRateYear5to30];
    [dataItem setObject:commerce forKey:kMoneyRateCosignTypeCommerce];
    [commerce release];
    accumulationFund = [[NSMutableDictionary alloc] init];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.04] forKey:kMoneyRateYear1to5];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.045] forKey:kMoneyRateYear5to30];
    [dataItem setObject:kMoneyRateCosignTypeAccumulationFund forKey:accumulationFund];
    [accumulationFund release];
    //[dataItem setObject:[NSNumber numberWithInt:14] forKey:@"value"];
    [self.moneyRateData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"11年2月9日利率下限(7折)" forKey:@"label"];
    commerce = [[NSMutableDictionary alloc] init];
    [commerce setObject: [NSNumber numberWithFloat:0.0452] forKey:kMoneyRateYear1to5];
    [commerce setObject:[NSNumber numberWithFloat:0.0462] forKey:kMoneyRateYear5to30];
    [dataItem setObject:commerce forKey:kMoneyRateCosignTypeCommerce];
    [commerce release];
    accumulationFund = [[NSMutableDictionary alloc] init];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.04] forKey:kMoneyRateYear1to5];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.045] forKey:kMoneyRateYear5to30];
    [dataItem setObject:kMoneyRateCosignTypeAccumulationFund forKey:accumulationFund];
    [accumulationFund release];
    //[dataItem setObject:[NSNumber numberWithInt:13] forKey:@"value"];
    [self.moneyRateData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"11年2月9日基准利率" forKey:@"label"];
    commerce = [[NSMutableDictionary alloc] init];
    [commerce setObject: [NSNumber numberWithFloat:0.0645] forKey:kMoneyRateYear1to5];
    [commerce setObject:[NSNumber numberWithFloat:0.066] forKey:kMoneyRateYear5to30];
    [dataItem setObject:commerce forKey:kMoneyRateCosignTypeCommerce];
    [commerce release];
    accumulationFund = [[NSMutableDictionary alloc] init];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.04] forKey:kMoneyRateYear1to5];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.045] forKey:kMoneyRateYear5to30];
    [dataItem setObject:kMoneyRateCosignTypeAccumulationFund forKey:accumulationFund];
    [accumulationFund release];
    //[dataItem setObject:[NSNumber numberWithInt:12] forKey:@"value"];
    [self.moneyRateData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"10年12月26日利率上限(1.1倍)" forKey:@"label"];
    commerce = [[NSMutableDictionary alloc] init];
    [commerce setObject: [NSNumber numberWithFloat:0.0684] forKey:kMoneyRateYear1to5];
    [commerce setObject:[NSNumber numberWithFloat:0.0704] forKey:kMoneyRateYear5to30];
    [dataItem setObject:commerce forKey:kMoneyRateCosignTypeCommerce];
    [commerce release];
    accumulationFund = [[NSMutableDictionary alloc] init];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.0375] forKey:kMoneyRateYear1to5];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.043] forKey:kMoneyRateYear5to30];
    [dataItem setObject:kMoneyRateCosignTypeAccumulationFund forKey:accumulationFund];
    [accumulationFund release];
    //[dataItem setObject:[NSNumber numberWithInt:11] forKey:@"value"];
    [self.moneyRateData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"10年12月26日利率下限(7折)" forKey:@"label"];
    commerce = [[NSMutableDictionary alloc] init];
    [commerce setObject: [NSNumber numberWithFloat:0.0435] forKey:kMoneyRateYear1to5];
    [commerce setObject:[NSNumber numberWithFloat:0.0448] forKey:kMoneyRateYear5to30];
    [dataItem setObject:commerce forKey:kMoneyRateCosignTypeCommerce];
    [commerce release];
    accumulationFund = [[NSMutableDictionary alloc] init];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.0375] forKey:kMoneyRateYear1to5];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.043] forKey:kMoneyRateYear5to30];
    [dataItem setObject:kMoneyRateCosignTypeAccumulationFund forKey:accumulationFund];
    [accumulationFund release];
    //[dataItem setObject:[NSNumber numberWithInt:10] forKey:@"value"];
    [self.moneyRateData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"10年12月26日基准利率" forKey:@"label"];
    commerce = [[NSMutableDictionary alloc] init];
    [commerce setObject: [NSNumber numberWithFloat:0.0622] forKey:kMoneyRateYear1to5];
    [commerce setObject:[NSNumber numberWithFloat:0.064] forKey:kMoneyRateYear5to30];
    [dataItem setObject:commerce forKey:kMoneyRateCosignTypeCommerce];
    [commerce release];
    accumulationFund = [[NSMutableDictionary alloc] init];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.0375] forKey:kMoneyRateYear1to5];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.043] forKey:kMoneyRateYear5to30];
    [dataItem setObject:kMoneyRateCosignTypeAccumulationFund forKey:accumulationFund];
    [accumulationFund release];
    //[dataItem setObject:[NSNumber numberWithInt:9] forKey:@"value"];
    [self.moneyRateData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"10年10月20日利率上限(1.1倍)" forKey:@"label"];
    commerce = [[NSMutableDictionary alloc] init];
    [commerce setObject: [NSNumber numberWithFloat:0.0656] forKey:kMoneyRateYear1to5];
    [commerce setObject:[NSNumber numberWithFloat:0.0675] forKey:kMoneyRateYear5to30];
    [dataItem setObject:commerce forKey:kMoneyRateCosignTypeCommerce];
    [commerce release];
    accumulationFund = [[NSMutableDictionary alloc] init];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.035] forKey:kMoneyRateYear1to5];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.0405] forKey:kMoneyRateYear5to30];
    [dataItem setObject:kMoneyRateCosignTypeAccumulationFund forKey:accumulationFund];
    [accumulationFund release];
    //[dataItem setObject:[NSNumber numberWithInt:8] forKey:@"value"];
    [self.moneyRateData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"10年10月20日基准利率" forKey:@"label"];
    commerce = [[NSMutableDictionary alloc] init];
    [commerce setObject: [NSNumber numberWithFloat:0.0596] forKey:kMoneyRateYear1to5];
    [commerce setObject:[NSNumber numberWithFloat:0.0614] forKey:kMoneyRateYear5to30];
    [dataItem setObject:commerce forKey:kMoneyRateCosignTypeCommerce];
    [commerce release];
    accumulationFund = [[NSMutableDictionary alloc] init];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.035] forKey:kMoneyRateYear1to5];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.0405] forKey:kMoneyRateYear5to30];
    [dataItem setObject:kMoneyRateCosignTypeAccumulationFund forKey:accumulationFund];
    [accumulationFund release];
    //[dataItem setObject:[NSNumber numberWithInt:7] forKey:@"value"];
    [self.moneyRateData addObject:dataItem];
    [dataItem release];
    
    dataItem = [[NSMutableDictionary alloc] init];
    [dataItem setObject:@"10年10月20日利率下限(85折)" forKey:@"label"];
    commerce = [[NSMutableDictionary alloc] init];
    [commerce setObject: [NSNumber numberWithFloat:0.0507] forKey:kMoneyRateYear1to5];
    [commerce setObject:[NSNumber numberWithFloat:0.0522] forKey:kMoneyRateYear5to30];
    [dataItem setObject:commerce forKey:kMoneyRateCosignTypeCommerce];
    [commerce release];
    accumulationFund = [[NSMutableDictionary alloc] init];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.035] forKey:kMoneyRateYear1to5];
    [accumulationFund setObject:[NSNumber numberWithFloat:0.0405] forKey:kMoneyRateYear5to30];
    [dataItem setObject:kMoneyRateCosignTypeAccumulationFund forKey:accumulationFund];
    [accumulationFund release];
    //[dataItem setObject:[NSNumber numberWithInt:6] forKey:@"value"];
    [self.moneyRateData addObject:dataItem];
    [dataItem release];
    
    
}

#pragma mark - Change calculate method view

- (void)showHouseAreaCalculate
{
    [UIView beginAnimations:@"showHouseAreaCalculate" context:NULL];
    [UIView setAnimationDuration:0.5];
    
    self.houseAreaView.alpha = 1.0;
    self.moneyAmountView.alpha = 0.0;
    
    CGRect frame = self.calculateView.frame;
    frame.origin.y = self.houseAreaView.frame.origin.y + self.houseAreaView.frame.size.height + 10.0;
    self.calculateView.frame = frame;
    
    [UIView commitAnimations];
}

- (void)showMoneyAmountCalcute
{
    [UIView beginAnimations:@"showMoneyAmountCalcute" context:NULL];
    [UIView setAnimationDuration:0.5];
    
    self.houseAreaView.alpha = 0.0;
    self.moneyAmountView.alpha = 1.0;
    
    CGRect frame = self.calculateView.frame;
    frame.origin.y = self.moneyAmountView.frame.origin.y + self.moneyAmountView.frame.size.height + 10.0;
    self.calculateView.frame = frame;
    
    [UIView commitAnimations];
}

#pragma mark - Show Action sheet

- (void)showRepaymentPrecent
{
    self.pickerViewType = kPickerViewTypeRepaymentPrecent;
    [self.pickerView reloadAllComponents];
    [self.pickerView selectRow:self.selectedRepaymentPrecentDataIndex inComponent:0 animated:NO];
    [self.actionSheet showInView:self.view];
    [self.actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
    
}

- (void)showRepaymentYear
{
    self.pickerViewType = kPickerViewTypeRepaymentYear;
    [self.pickerView reloadAllComponents];
    [self.pickerView selectRow:self.selectedRepaymentYearDataIndex inComponent:0 animated:NO];
    [self.actionSheet showInView:self.view];
    [self.actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}

- (void)showMoneyRate
{
    self.pickerViewType = kPickerViewTypeMoneyRate;
    [self.pickerView reloadAllComponents];
    [self.pickerView selectRow:self.selectedMoneyRateDataIndex inComponent:0 animated:NO];
    [self.actionSheet showInView:self.view];
    [self.actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}

- (void)setSelectedRepaymentPrecentDataWithIndex:(int)index
{
    self.selectedRepaymentPrecentDataIndex = index;
    NSString *label = [[self.repaymentPrecentData objectAtIndex:index] objectForKey:@"label"];
    [self.repaymentPrecentButton setTitle:label forState:UIControlStateNormal];
    
}

- (void)setSelectedRepaymentYearDataWithIndex:(int)index
{
    self.selectedRepaymentYearDataIndex = index;
    NSString *label = [[self.repaymentYearData objectAtIndex:index] objectForKey:@"label"];
    [self.repaymentYearButton setTitle:label forState:UIControlStateNormal];
    
}

- (void)setSelectedMoneyRateDataWithIndex:(int)index
{
    self.selectedMoneyRateDataIndex = index;
    NSString *label = [[self.moneyRateData objectAtIndex:index] objectForKey:@"label"];
    [self.moneyRateButton setTitle:label forState:UIControlStateNormal];    
}

#pragma mark - Calculate result

- (float)calculateHouseMoneyAmount
{
    int houseAreaPrice = [self.houseAreaPriceTextField.text intValue];
    int houeseArea = [self.houseAreaTextField.text intValue];
    float houseMoneyAmount = houseAreaPrice*houeseArea;
    
    return houseMoneyAmount;
}

- (float)calculateCosignAmount
{
    float houseMoneyAmount = [self calculateHouseMoneyAmount];
    float cosignAmount = 0.0;
    
    if (self.calculateSegmentController.selectedSegmentIndex == 0) {
        float repaymentPrecent = [[[self.repaymentPrecentData objectAtIndex:self.selectedRepaymentPrecentDataIndex] objectForKey:@"value"] floatValue]/10.0;
        cosignAmount = houseMoneyAmount *repaymentPrecent;
        
    } else {
        cosignAmount = [self.moneyAmountTextField.text floatValue];
    }

    return cosignAmount;
}

- (float)calculateRepaymentPreMonth1
{
    int repaymentYear = [[[self.repaymentYearData objectAtIndex:self.selectedRepaymentYearDataIndex] objectForKey:@"value"] intValue];
    int repaymentMonth = repaymentYear*12;
    
    NSMutableDictionary *moneyRateItem = [self.moneyRateData objectAtIndex:self.selectedMoneyRateDataIndex];
    float moneyRate = 0.0;
    if (self.repaymentCategorySegmentControl.selectedSegmentIndex == 0) {
        NSMutableDictionary *commerce = [moneyRateItem objectForKey:kMoneyRateCosignTypeCommerce];
        if (repaymentYear <= 5) {
            moneyRate = [[commerce objectForKey:kMoneyRateYear1to5] floatValue];
        } else {
            moneyRate = [[commerce objectForKey:kMoneyRateYear5to30] floatValue];
        }
    } else if (self.repaymentCategorySegmentControl.selectedSegmentIndex == 1) {
        NSMutableDictionary *accumulateionFund = [moneyRateItem objectForKey:kMoneyRateCosignTypeAccumulationFund];
        if (repaymentYear <= 5) {
            moneyRate = [[accumulateionFund objectForKey:kMoneyRateYear1to5] floatValue];
        } else {
            moneyRate = [[accumulateionFund objectForKey:kMoneyRateYear5to30] floatValue];
        }
    }
    
    NSLog(@"利率 %f", moneyRate);
    
    float moneyRatePreMonth = moneyRate/12;
    
    float cosignAmount = [self calculateCosignAmount];
    float repaymentPreMonth = (cosignAmount*moneyRatePreMonth*pow((1+moneyRatePreMonth), repaymentMonth))/(pow((1+moneyRatePreMonth), repaymentMonth)-1);
    
    return repaymentPreMonth;
}

- (float)calculateRepaymentAmount1
{
    float repaymentPreMonth = [self calculateRepaymentPreMonth1];
    float repaymentMonth = [self calculateRepaymentMonth];
    float repaymentAmount = repaymentPreMonth*repaymentMonth;
    
    return repaymentAmount;

}


- (NSMutableArray *)calculateRepaymentPreMonth2
{
    int repaymentYear = [[[self.repaymentYearData objectAtIndex:self.selectedRepaymentYearDataIndex] objectForKey:@"value"] intValue];
    int repaymentMonth = repaymentYear*12;
    float cosignAmount = [self calculateCosignAmount];
    float moneyPreMonth = cosignAmount/repaymentMonth;
    
    NSMutableDictionary *moneyRateItem = [self.moneyRateData objectAtIndex:self.selectedMoneyRateDataIndex];
    float moneyRate = 0.0;
    if (self.repaymentCategorySegmentControl.selectedSegmentIndex == 0) {
        NSMutableDictionary *commerce = [moneyRateItem objectForKey:kMoneyRateCosignTypeCommerce];
        if (repaymentYear <= 5) {
            moneyRate = [[commerce objectForKey:kMoneyRateYear1to5] floatValue];
        } else {
            moneyRate = [[commerce objectForKey:kMoneyRateYear5to30] floatValue];
        }
    } else if (self.repaymentCategorySegmentControl.selectedSegmentIndex == 1) {
        NSMutableDictionary *accumulateionFund = [moneyRateItem objectForKey:kMoneyRateCosignTypeAccumulationFund];
        if (repaymentYear <= 5) {
            moneyRate = [[accumulateionFund objectForKey:kMoneyRateYear1to5] floatValue];
        } else {
            moneyRate = [[accumulateionFund objectForKey:kMoneyRateYear5to30] floatValue];
        }
    }
    
    NSLog(@"利率 %f", moneyRate);
    
    float moneyRatePreMonth = moneyRate/12;
    
    float totalRayment = 0.0;
    
    NSMutableArray *months = [[NSMutableArray alloc] initWithCapacity:repaymentMonth];
    for (int i=0; i<repaymentMonth; i++) {
        float accuralPreMonth = (cosignAmount-totalRayment)*moneyRatePreMonth;
        float repaymentPreMonth = moneyPreMonth + accuralPreMonth;
        [months addObject:[NSNumber numberWithFloat:repaymentPreMonth]];
        totalRayment += repaymentPreMonth;
    }
    
    return months;
}

- (float)calculateRepaymentAmount2
{
    NSMutableArray *repaymentMonths = [self calculateRepaymentPreMonth2];
    float repaymentAmount = 0.0;
    for (int i=0; i<[repaymentMonths count]; i++) {
        float repaymentPreMonth = [[repaymentMonths objectAtIndex:i] floatValue];
        repaymentAmount +=repaymentPreMonth;
    }
    
    return repaymentAmount;
}

- (float)calculateAccrual
{
    float cosignAmount = [self calculateCosignAmount];
    float repaymentAmount = [self calculateRepaymentAmount1];
    float accural = repaymentAmount-cosignAmount;
    return accural;
}

- (float)calculateFirstMoney
{
    float houseMoneyAmount = [self calculateHouseMoneyAmount];
    float fisrtMoney = houseMoneyAmount*0.3;
    return fisrtMoney;
}

- (int)calculateRepaymentMonth
{
    int repaymentYear = [[[self.repaymentYearData objectAtIndex:self.selectedRepaymentYearDataIndex] objectForKey:@"value"] intValue];
    int repaymentMonth = repaymentYear*12;
    
    return repaymentMonth;
}


@end
